import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
 
public class IKAnalyzerUtil {
	/**
	 * 对一段文字进行分词，获得一个相应的单词列表
	 * @param sentence
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	public static ArrayList<String> Seg(String sentence) throws IOException {
		ArrayList<String> words=new ArrayList();
		String text = "";
		// 创建分词对象
		Analyzer anal = new IKAnalyzer(true);
		StringReader reader = new StringReader(sentence);
		// 分词
		TokenStream ts = anal.tokenStream("", reader);

		ts.reset();
		CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
		// 遍历分词数据
		while (ts.incrementToken()) {
			//text += term.toString() + "|";
			words.add(term.toString());
			System.out.println(term.toString());
		}
		reader.close();
		return words;
	}
 
	@SuppressWarnings("static-access")
	public static void main(String[] args) throws IOException {
		//IKAnalyzerTest ik = new IKAnalyzerTest();
		//System.out.println(ik.Seg("三峡人家-清江画廊动车2日游吊脚楼民歌和土家幺妹住商圈酒店跟"));
	}
	
	
	
	
	
}
